
//const config = require('../config/config')
const config = require('../config/production')
const mysql  = require('mysql');
const connection = mysql.createConnection({
    host     : config.host,
    user     : config.user,
    password : config.pwd,
    database : config.database,
    charset  : 'urf-8'
  });

  connection.connect();

//增加方法
exports.addData = (query,Params,resultfunc)=>{
    //var  addSql = 'INSERT INTO websites(Id,name,url,alexa,country) VALUES(0,?,?,?,?)';
    
    //var  addSqlParams = ['菜鸟工具', 'https://c.runoob.com','23453', 'CN'];
 
    let addSql = query
    let addSqlParams = Params
 
    connection.query(addSql,addSqlParams,function (err, result) {
        if(err){
            console.log('[INSERT ERROR] - ',err.message);
            resultfunc(err.message)
            return;
        }else{
            console.log('INSERT ID:',result);
            resultfunc(result)
            return;
        }        
    });

}

//查询
exports.queryDate = (querysql,resultfunc)=>{

    var  sql = querysql
    connection.query(sql,function (err, result) {
        if(err){
            console.log('[SELECT ERROR] - ',err.message);
            resultfunc(err.message,null)
            return;
        }else{
            resultfunc(null,result)
            return;
        }
    });

}


//修改数据
exports.updateData = (updataSql,updateParme,resultfunc)=>{
    var modSql = updataSql
    var modSqlParams = updateParme

    connection.query(modSql,modSqlParams,function (err, result) {
    if(err){
        console.log('[UPDATE ERROR] - ',err.message);
        resultfunc('[UPDATE ERROR] - ',err.message)
         return;
    }else{
        console.log('UPDATE affectedRows',result.affectedRows);
        resultfunc('UPDATE affectedRows',result.affectedRows)
    }        
    });

}

//删除数据

exports.deleteDate = (deleteSql,resultfunc)=>{

    var delSql = deleteSql
        connection.query(delSql,function (err, result) {
            if(err){
                console.log('[DELETE ERROR] - ',err.message);
                resultfunc('[DELETE ERROR] - ',err.message)
                return;
            }else{
                console.log('DELETE affectedRows',result.affectedRows);
                resultfunc('DELETE affectedRows',result.affectedRows)
            }    
    })
}























